package com.scs.application.modules.price.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UtilCommon;
import com.scs.application.modules.price.entity.AdjustmentItem;
import com.scs.application.modules.price.mapper.AdjustmentItemMapper;
import com.scs.application.modules.price.service.AdjustmentItemService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class AdjustmentItemServiceImpl extends BaseServiceImpl<AdjustmentItemMapper, AdjustmentItem> implements AdjustmentItemService {

    @Override
    public boolean saveOrUpdate(AdjustmentItem entity) {
        int count = this.count(
                Wrappers.<AdjustmentItem>query()
                        .eq("matr_id", entity.getMatrId()).eq("adjust_id",entity.getAdjustId())
                        .ne(StringUtils.isNotBlank(entity.getId()),"id",entity.getId())
        );
        if (count > 0) {
            throw new BusinessException("该单据明细已存在耗材：%s ，不能重复添加",entity.getMatrName());
        }

        //调价后HRP编码、调价后HRP编码、调价后单品单价 必须发生变化，才允许调整
        if (
                UtilCommon.getStr(entity.getHrpCodeAfter(),"").equals(UtilCommon.getStr(entity.getHrpCodeBefore(),""))
                && UtilCommon.getStr(entity.getHisCodeAfter(),"").equals(UtilCommon.getStr(entity.getHisCodeBefore(),""))
                && entity.getSkuPriceChange() == 0
        ) {
            BusinessException.throwValidFail("调价后HRP编码、调价后HRP编码、调价后单品单价三个属性未发生变化，不需要保存");
        }

        return super.saveOrUpdate(entity);
    }
}
